package com.imay.live.utils;

import android.Manifest;
import android.content.Context;
import android.os.Environment;

import com.imay.live.IMApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by jeaman on 2017/3/30.
 */

public class FileUtil {
    /**
     * 从assets目录下拷贝整个文件夹，不管是文件夹还是文件都能拷贝
     *
     * @param context           上下文
     * @param rootDirFullPath   要拷贝的目录
     * @param targetDirFullPath 目标目录
     */
    public static void copyFolderFromAssets(Context context, String rootDirFullPath, String targetDirFullPath) {
        try {
            String[] listFiles = context.getAssets().list(rootDirFullPath);// 遍历该目录下的文件和文件夹
            for (String string : listFiles) {// 看起子目录是文件还是文件夹，这里只好用.做区分了
                if (isFileByName(string)) {// 文件
                    copyFileFromAssets(context, rootDirFullPath + File.separator + string, targetDirFullPath + File.separator + string);
                } else {// 文件夹
                    String childRootDirFullPath = rootDirFullPath + File.separator + string;
                    String childTargetDirFullPath = targetDirFullPath + File.separator + string;
                    new File(childTargetDirFullPath).mkdirs();
                    copyFolderFromAssets(context, childRootDirFullPath, childTargetDirFullPath);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static boolean isFileByName(String string) {
        if (string.contains(".")) {
            return true;
        }
        return false;
    }

    /**
     * 从assets目录下拷贝文件
     *
     * @param context 上下文
     * @param assetsFilePath 文件的路径
     * @param targetFileFullPath 目标文件路径
     */
    public static void copyFileFromAssets(Context context, String assetsFilePath, String targetFileFullPath) {
        InputStream inputStream;
        try {
            inputStream = context.getAssets().open(assetsFilePath);
            copyFile(inputStream, targetFileFullPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean copyFile(InputStream ins, String destFileFullPath) {
        FileOutputStream fos = null;
        try {
            File file = new File(destFileFullPath);
            fos = new FileOutputStream(file);
            byte[] buffer = new byte[8192];
            int count = 0;
            while ((count = ins.read(buffer)) > 0) {
                fos.write(buffer, 0, count);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if(fos != null){
                    fos.close();
                }
                if(ins != null){
                    ins.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static String getDiskCacheDir() {
        String cachePath = null;
        if (!PermissionUtil.selfPermissionGranted(IMApplication.getInstance(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            return IMApplication.getInstance().getCacheDir().getPath();
        }
        if (!PermissionUtil.selfPermissionGranted(IMApplication.getInstance(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
            return IMApplication.getInstance().getCacheDir().getPath();
        }
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            if (IMApplication.getInstance().getExternalCacheDir() == null) {
                cachePath = IMApplication.getInstance().getCacheDir().getPath();
            } else {
                cachePath = IMApplication.getInstance().getExternalCacheDir().getPath();
            }
        } else {
            cachePath = IMApplication.getInstance().getCacheDir().getPath();
        }
        return cachePath;
    }
}
